﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace MarpelPharma.BarcodeClasses
{
    public partial class BarcodeReaderControl : UserControl
    {

        #region Attributes

        private Barcode _barcodeObject;
        private bool _isExecuted;
        private string _errorMessage;

        #endregion

        #region Properties

        public string ErrorMessage
        {
            get { return _errorMessage; }
            set { _errorMessage = value; }
        }

        public bool IsExecuted
        {
            get { return _isExecuted; }
            set { _isExecuted = value; }
        }

        internal Barcode BarcodeObject
        {
            get { return _barcodeObject; }
            set { _barcodeObject = value; }
        }

        #endregion

        #region Constructor

        public BarcodeReaderControl()
        {
            InitializeComponent();
        }

        #endregion

        #region Events

        /// <summary>
        /// Event to be raised by the parent form
        /// </summary>
        public event EventHandler BarcodeExecuted;


        /// <summary>
        /// Event that creates make a call to the webservice, and create a
        /// BarcodeObject to be use and access from the parent form
        /// and calls the event on the parent after the object is created
        /// </summary>
        private void BarcodeExecutedEvent()
        {
            string barCodeRead = txtBarcode.Text.Trim();
            createBarcodeObject(barCodeRead);
            //Null check makes sure the parent form is attached to the event
            if (this.BarcodeExecuted != null)
                this.BarcodeExecuted(new object(), new EventArgs());
        }


        /// <summary>
        /// Event that control the key up function from the parent form
        /// when the Enter Key is detected the event is raised to the parent
        /// after it creates the object needed
        /// </summary>
        /// <param name="sender">Parameter that contains the reference to the control that raised the vent</param>
        /// <param name="e">Parameter that contains the event data</param>
        public void BarcodeReaderControl_KeyUp(object sender, KeyEventArgs e)
        {
            txtBarcode.Text = txtBarcode.Text.Replace('!', ' ');
            //Check if the key press is the last digit of the Barcode
            if (e.KeyCode == Keys.Enter)
            {
                if(txtBarcode.Text.Trim() != string.Empty)
                    BarcodeExecutedEvent();
            }
        }


        /// <summary>
        /// Event that control the key down function from the parent form
        /// when the special character ! is detected it says that the key is comming from
        /// the barcode scanner and sets the focus to the texbox
        /// </summary>
        /// <param name="sender">Parameter that contains the reference to the control that raised the vent</param>
        /// <param name="e">Parameter that contains the event data</param>
        public void BarcodeReaderControl_KeyPress(object sender, KeyPressEventArgs e)
        {
            //Check if the key press is the last digit of the Barcode
            if (e.KeyChar == '!')
            {
                txtBarcode.Text = string.Empty;
                txtBarcode.Focus();
            }
        }


        #endregion

        #region Methods

        /// <summary>
        /// Method in charge of making a query to the webservice and create an object to
        /// be later used by the parent form
        /// </summary>
        /// <param name="barcode">String parameter containing the barcode to be use for the query</param>
        private void createBarcodeObject(string barcode)
        {

            //Call webservice
           
        }

        #endregion

    }
}
